{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(442, 1)\n",
      "first: (422, 1)\n",
      "second: (422, 2)\n",
      "Mean Squared Error: 2548.0723987259707\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHUNJREFUeJzt3X2UXHWd5/H3t0N46IBCTIghD1VxCCxRnAgtxBkWeZiswhyNjDDg6eHBZWw9wp7xDMcD2usDe+wDoqODw4HZRlgTUoASRmUV9wywILhHwU5CIiELCaE76SQkIaASmk1M+rt/3FvpolMPt6qr6lbd+3mdU6erb/2q+ts3VZ/8+vf73XvN3RERkeTqiLsAERFpLAW9iEjCKehFRBJOQS8iknAKehGRhFPQi4gknIJeRCThFPQiIgmnoBcRSbjD4i4AYNq0aZ7NZuMuQ0SkraxcufJVd59eqV1LBH02m2VgYCDuMkRE2oqZDUVpp6EbEZGEU9CLiCScgl5EJOEU9CIiCaegFxFJOAW9iEiNcrkc2WyWjo4OstksuVwu7pKKaonllSIi7SaXy9HT08PIyAgAQ0ND9PT0ANDd3R1naYdQj15EpAa9vb0HQz5vZGSE3t7emCoqTUEvIlKDzZs3V7U9Tgp6EZEazJ07t6rtcVLQi4jUoK+vj87Ozrdt6+zspK+vL6aKSlPQi4jUoLu7m/7+fjKZDGZGJpOhv7+/5SZiAczdyzcwOxJ4EjiCYJXOCnf/mpnNA+4HpgKrgMvdfZ+ZHQEsA04HdgOXuvtguZ/R1dXlOqmZiEh1zGylu3dVahelR78XOM/d/xxYCHzUzBYB3wS+6+7zgdeBq8P2VwOvu/uJwHfDdiIiEpOKQe+BP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zC/8SmA1sK9Fm/P4o99xKr9nMOjGz2cCPgSvc/aX8E8q8B5peZ/iX796wnpVm9hJwUti+cLgu9v0ZuoxxvfmY9me5554z7rlPUOv+rNfEQ6vcgG/x9snDW4q0mQq8TDDJcVx4f2rB4zcDN457zsyC+xcBv4mrToL/oKeFbSYTjEF+Lvz+Gt4+CfWjOPcn8A2C3lFHvfdnuB82EUym5ie73juuTdH9AbyXt0/GbiKYPKv4mk2u89iw/SeLvGbR90BMdU4HJoX33wNsLXgP/BZYxNjk4YVx1Rl+30EQmO+Je38WtP0Bh07GlvpMVb0/a/4lWvVGMA73GLAh/JrfOV3A9wva/WeCCbiNwKfHvcYm4D+M23YTwWTYGuDx8Y83s05gCsGKoLVhTbcWfMiOBB4I2z9T+GaOoc7ZBH9WrgeeDW9/X8/9CVwIvEiwuqE33PbfgI9X2h8EQ1MvAS9QsHKh2GvW4X1ZU53AfwXeLNh/zxLMk5R8D8RU5ycL/j1XAR8reM0u4LnwNW8jPFAzjjrDx85hXMcixv35QYL/dN4EdgPryn2mat2fOjJWRCThkrjqRkRECijoRUQSTkEvIpJwCnoRkYRT0IuIJJyCXkQk4RT0IiIJp6AXEUm4/w/uze3A0WX/5QAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# coding: utf-8\n",
    "\n",
    "get_ipython().run_line_magic('matplotlib', 'inline')\n",
    "import matplotlib.pylab as plt\n",
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "\n",
    "class LinearRegression():\n",
    "    def __init__(self):\n",
    "        self.w = None\n",
    "        \n",
    "    def fit(self, X, y):\n",
    "        # Insert constant ones for bias weights\n",
    "        print(\"first:\", X.shape)\n",
    "        # 在第0项，插入 1，让x0项为1\n",
    "        X = np.insert(X, 0, 1, axis=1)\n",
    "        print(\"second:\", X.shape)\n",
    "        # inv(): 对当前值取逆， dot()：矩阵计算\n",
    "        X_ = np.linalg.inv(X.T.dot(X))\n",
    "        # 算出来 最好的 一组参数 theta\n",
    "        self.w = X_.dot(X.T).dot(y)\n",
    "    \n",
    "    def predict(self, X):\n",
    "        # Insert constant ones for bias weights\n",
    "        X = np.insert(X, 0, 1, axis=1)\n",
    "        y_pred = X.dot(self.w)\n",
    "        return y_pred\n",
    "\n",
    "    \n",
    "def mean_squared_error(y_true, y_pred):\n",
    "    mse = np.mean(np.power(y_true - y_pred, 2))\n",
    "    return mse\n",
    "\n",
    "\n",
    "def main():\n",
    "    # load the diabetes dataset\n",
    "    diabetes = datasets.load_diabetes()\n",
    "    \n",
    "    # Use only one feature\n",
    "    X = diabetes.data[: , np.newaxis, 2]\n",
    "    print(X.shape)\n",
    "    # split the data into training/testing sets\n",
    "    x_train, x_test = X[:-20], X[-20:]\n",
    "    \n",
    "    # Split the targets into training/testing sets\n",
    "    y_train, y_test = diabetes.target[:-20], diabetes.target[-20:]\n",
    "    \n",
    "    clf = LinearRegression()\n",
    "    clf.fit(x_train, y_train)\n",
    "    y_pred = clf.predict(x_test)\n",
    "    \n",
    "    # Print the mean squared error\n",
    "    print(\"Mean Squared Error:\", mean_squared_error(y_test, y_pred))\n",
    "    \n",
    "    # Plot the results\n",
    "    plt.scatter(x_test[: , 0], y_test, color='black')\n",
    "    plt.plot(x_test[:, 0], y_pred, color=\"blue\", linewidth=3)\n",
    "    plt.show()\n",
    "\n",
    "    \n",
    "main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-20-812567782fbe>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-20-812567782fbe>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    X = diabetes.data[: , np.newaxis, 2]\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "diabetes = datasets.load_diabetes()\n",
    "print(diabetes.data)\n",
    "X = diabetes.data[: , np.newaxis, 2]\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
